package cn.machen.www.fashion.controller;

import cn.machen.www.fashion.pojo.Cart;
import cn.machen.www.fashion.pojo.CartItem;
import cn.machen.www.fashion.pojo.Category;
import cn.machen.www.fashion.pojo.Product;
import cn.machen.www.fashion.services.ProductService;
import cn.machen.www.fashion.utils.BaseServlet;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.rowset.BaseRowSet;
import java.io.IOException;

public class CartServlet extends BaseServlet {


    /**
     * 添加购物条目
     */

    public String add(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

            //获取到购物车
        Cart cart = (Cart)request.getSession().getAttribute("cart");

            //得到商品和购买的数量
        String pid = request.getParameter("pid");
        Product product = new ProductService().findSingleProduct(pid);
        int count = Integer.parseInt(request.getParameter("count"));

        CartItem cartItem = new CartItem();
        cartItem.setCount(count);
        cartItem.setSize(request.getParameter("size"));   //获得商品的size
        cartItem.setProduct(product);

        System.out.println("size"+cartItem.getSize());
        //将条目添加到购物车中
        cart.add(cartItem);
        request.getRequestDispatcher("/cart.jsp").forward(request,response);
        return null;
    }
    /**
     * 清空购物车
     */
    public String clear(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        Cart cart = (Cart) request.getSession().getAttribute("cart");
        cart.clear();
        request.getRequestDispatcher("/cart.jsp").forward(request,response);
        return null;
    }



    /**
     * 删除购物车的条目
     */
    public String delete(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        Cart cart = (Cart)request.getSession().getAttribute("cart");
        String pid = request.getParameter("pid");
        cart.delete(pid);
        request.getRequestDispatcher("/cart.jsp").forward(request,response);
        return null;
    }


}
